using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Keep.Const;
using Keep.Model;
using Keep.Service;
using Microsoft.AspNetCore.Mvc;
using Serilog;

namespace Keep.Controllers;

[ApiController]
[Route("api/[controller]/[action]")]
public class LinksController : ControllerBase
{

    private readonly ILinkService linkService;

    private readonly IFileService fileService;

    public LinksController(ILinkService linkService, IFileService fileService)
    {
        this.linkService = linkService;
        this.fileService = fileService;
    }

    [HttpPost(Name = "SelLinks")]
    public async Task<ActionResult<ResultData>> SelLinks(Links links)
    {
        Log.Warning("IN");
        List<Links> sys = await Task.Run(() => linkService.SelLinks(links));
        Log.Warning("OUT => " + sys.ToString() + "  数量:" + sys.Count);
        return Ok(new ResultData(Static.Enable, sys));
    }

    [HttpPost(Name = "AddLinks")]
    public async Task<ActionResult<ResultData>> AddLinks(Links links)
    {
        Log.Warning("IN");
        bool sys = await Task.Run(() => linkService.AddLinks(links));
        Log.Warning("OUT => " + sys);
        return Ok(new ResultData(Static.Enable, sys));
    }
    [HttpPost(Name = "AddLinksClick")]
    public async Task<ActionResult<ResultData>> AddLinksClick(Links links)
    {
        Log.Warning("IN");
        await Task.Run(() => linkService.AddLinkClick((int)links.id));
        Log.Warning("OUT");
        return Ok(new ResultData(Static.Enable, true));
    }

    [HttpPost(Name = "EditLinks")]
    public async Task<ActionResult<ResultData>> EditLinks(Links links)
    {
        Log.Warning("IN");
        bool sys = await Task.Run(() => linkService.EditLinks(links));
        Log.Warning("OUT => " + sys);
        return Ok(new ResultData(Static.Enable, sys));
    }

    [HttpPost(Name = "DelLinks")]
    public async Task<ActionResult<ResultData>> DelLinks(Links links)
    {
        Log.Warning("IN");
        bool sys = await Task.Run(() => linkService.DelLinks((int)links.id));
        Log.Warning("OUT => " + sys);
        return Ok(new ResultData(Static.Enable, sys));
    }

    [HttpPost(Name = "UploadIconImg")]
    [RequestSizeLimit(long.MaxValue)]
    public async Task<ActionResult<ResultData>> UploadIconImg(IFormFile file)
    {
        Log.Warning("UploadIconImg => IN");
        string sys = fileService.UploadIconImage(file);
        return Ok(new ResultData(!String.IsNullOrEmpty(sys) ? Static.Enable : Static.Disable, sys));
    }

}
